package com.cqc.jdk8.Jutil.concurrent.forkJoin;

import org.junit.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;

/**
 * @author cui
 * @date 2025/6/22 21:34
 * @description
 **/
public class ForkJoinTest {


    @Test
    public void f1() throws ExecutionException, InterruptedException {
        int[] array = new int[1999];
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            array[i] = i;
            sum += i;
        }
        System.out.println("for循环计算数组的和：sum:" + sum);

        ForkJoinPool forkJoinPool = new ForkJoinPool();
        ForkJoinTask<Integer> task = forkJoinPool.submit(new SumRecursiveTask(array, 0, array.length));
        System.out.println("sum:" + task.get());
        Integer sum2 = forkJoinPool.invoke(new SumRecursiveTask(array, 0, array.length));
        System.out.println("sum2:" + sum2);
    }


}
